
local linkToActorPathButton = nil
local cameraTrackButton = nil
local cameraShakeSlider = nil
local closeButton = nil

local function CreateLabel(text, color, x,y, parent)
	local label = vgui.Create("DLabel", parent) 
	label:SetText(text) 
	label:SetTextColor(color) 
	label:SizeToContents()
	label:SetPos(x,y)
	return label
end

local WHITE = Color(255,255,255,255)
local ORANGE = Color(195,140,45,255)
local DARK_GREY = Color(16,16,16,255)
local GREY = Color(128,128,128,255)

local PADDING = 9
local THIN_PAD = 1
local ANCHOR_Y = PADDING
local ANCHOR_X = PADDING
local CAT_WIDTH = 100
local SEC_WIDTH = 300
local HEIGHT = 250
local TITLE_BAR_HEIGHT = 20
local BUTTON_DIMENSION = 48
local BUTTON_DIMENSION_SMALL = 32
local lastPropertiesSection = 1
local lastNameNumber = 1

local function CreateButton(text, clickFunction, width,height, darken, parent)
	local button = vgui.Create("DButton", parent) 
	button:SetText(text) 
	button:SetTextColor(WHITE) 
	if darken then
		button:SetColor(DARK_GREY)
    end
	button:SizeToContents()
	button:SetTextColorHovered(ORANGE) 
	button:SetSize(width,height)
	button:SetDrawBorder(false)
	button.DoClick = clickFunction
	return button
end

local function DisplayModeSection(sectionPanel)

	local yPos = THIN_PAD
	local xPos = PADDING
	local mode = CreateLabel("Mode:", GREY, xPos,yPos,sectionPanel)
	yPos = yPos + THIN_PAD + mode:GetTall()

	local cameraTrackButton = vgui.Create( "DImageButton", sectionPanel) 
	cameraTrackButton:SetMaterial( "gui/camera_track" ) 
	cameraTrackButton:SetSize(BUTTON_DIMENSION,BUTTON_DIMENSION)
	cameraTrackButton:SetPos(xPos,yPos)
	cameraTrackButton.DoClick = function () 
		LocalPlayer():ConCommand( "npc_mover_gun_toolstate " .. CatmullRomCams.SToolMethods.ActorMover.TOOLSTATE_PLOTCAMERATRACK)
		sectionPanel:GetParent():SetVisible(false)
	end

	xPos = PADDING + cameraTrackButton:GetWide() + PADDING
	local camera_track_name = CreateLabel("Camera Track Name:", GREY, xPos,yPos,sectionPanel)

	local trackName = vgui.Create( "DTextEntry" ) 
	trackName:SetParent( sectionPanel )
			  
	trackName:SetText( string.format("MyTrack%u", LocalPlayer():GetInfoNum("default_camera_track_num")))
	trackName:SetPos( xPos, yPos + camera_track_name:GetTall() + THIN_PAD) 
	trackName:SetSize( 300 - cameraTrackButton:GetWide() - PADDING - PADDING - PADDING , 25 ) 
	trackName.OnEnter = function () 
		LocalPlayer():ConCommand( "npc_mover_gun_camera_track_name " .. trackName:GetValue())
		LocalPlayer():ConCommand( "npc_mover_gun_toolstate " .. CatmullRomCams.SToolMethods.ActorMover.TOOLSTATE_PLOTCAMERATRACK)
		sectionPanel:GetParent():SetVisible(false)
	end

	yPos = yPos + THIN_PAD + cameraTrackButton:GetTall()
	xPos = PADDING

	local actorPathwayButton = vgui.Create( "DImageButton", sectionPanel) 
	actorPathwayButton:SetMaterial( "gui/actor_path" ) 
	actorPathwayButton:SetSize(BUTTON_DIMENSION,BUTTON_DIMENSION)
	actorPathwayButton:SetPos(xPos,yPos)
	actorPathwayButton.DoClick = function () 
		LocalPlayer():ConCommand( "npc_mover_gun_toolstate " .. CatmullRomCams.SToolMethods.ActorMover.TOOLSTATE_PLOTACTORPATHWAY)
		sectionPanel:GetParent():SetVisible(false)
	end


	xPos = PADDING + actorPathwayButton:GetWide() + PADDING
	
	local actor_path_name = CreateLabel("Actor Path Name:", GREY, xPos,yPos,sectionPanel)
	
	local pathName = vgui.Create( "DTextEntry" ) 
	pathName:SetParent( sectionPanel )
	 
	pathName:SetText(string.format("MyPathway%u", LocalPlayer():GetInfoNum("default_actor_pathway_num")))
	pathName:SetPos( xPos, yPos + actor_path_name:GetTall() + THIN_PAD) 
	pathName:SetSize( 300 - actorPathwayButton:GetWide() - PADDING - PADDING - PADDING , 25 ) 
	pathName.OnEnter = function () 
	toolState = TOOLSTATE_IDLE
		LocalPlayer():ConCommand( "npc_mover_gun_actor_pathway_name " .. pathName:GetValue())
		LocalPlayer():ConCommand( "npc_mover_gun_toolstate " .. CatmullRomCams.SToolMethods.ActorMover.TOOLSTATE_PLOTACTORPATHWAY)
		sectionPanel:GetParent():SetVisible(false)
	end

	xPos = PADDING
	yPos = yPos + THIN_PAD + actorPathwayButton:GetTall()
	local particleSystemButton = vgui.Create( "DImageButton", sectionPanel) 
	particleSystemButton:SetMaterial( "gui/particle_system" ) 
	particleSystemButton:SetSize(BUTTON_DIMENSION,BUTTON_DIMENSION)
	particleSystemButton:SetPos(xPos,yPos)
	particleSystemButton.DoClick = function () 
		LocalPlayer():ConCommand( "npc_mover_gun_toolstate " .. CatmullRomCams.SToolMethods.ActorMover.TOOLSTATE_PLOTPARTICLESYSTEM)
		sectionPanel:GetParent():SetVisible(false)
	end

	xPos = PADDING + particleSystemButton:GetWide() + PADDING
	
	local particlesystem_name = CreateLabel("Particle System Name:", GREY, xPos,yPos,sectionPanel)

	local particlesystemName = vgui.Create( "DTextEntry" ) 
	particlesystemName:SetParent( sectionPanel )
	 
	particlesystemName:SetText(string.format("MyParticleSystem%u", LocalPlayer():GetInfoNum("default_actor_pathway_num")))
	particlesystemName:SetPos( xPos, yPos+THIN_PAD + particlesystem_name:GetTall()) 
	particlesystemName:SetSize( 300 - particleSystemButton:GetWide() - PADDING - PADDING - PADDING, 25 ) 
	particlesystemName.OnEnter = function () 
	toolState = TOOLSTATE_IDLE
		LocalPlayer():ConCommand( "npc_mover_gun_actor_pathway_name " .. pathName:GetValue())
		LocalPlayer():ConCommand( "npc_mover_gun_toolstate " .. CatmullRomCams.SToolMethods.ActorMover.TOOLSTATE_PLOTACTORPATHWAY)
		sectionPanel:GetParent():SetVisible(false)
	end

	yPos = yPos + particleSystemButton:GetTall() + THIN_PAD
	xPos = PADDING

		local resetButton = vgui.Create( "DImageButton", sectionPanel) 
	resetButton:SetMaterial( "gui/reset" ) 
	resetButton:SetSize(BUTTON_DIMENSION,BUTTON_DIMENSION)
	resetButton:SetPos(xPos,yPos)
	resetButton.DoClick = function () 
		LocalPlayer():ConCommand( "npc_mover_gun_toolstate " .. CatmullRomCams.SToolMethods.ActorMover.TOOLSTATE_IDLE)
		sectionPanel:GetParent():SetVisible(false)
	end

end

local function DisplayUtilitySection(sectionPanel)

	local yPos = THIN_PAD
	local mode = CreateLabel("Display:", GREY, PADDING,yPos,sectionPanel)
	yPos = yPos + THIN_PAD + mode:GetTall()

	local showFeatures = vgui.Create( "DButton", sectionPanel )
	showFeatures:SetPos(PADDING , yPos )
	showFeatures:SetText( "Show anchor points and links" )
	showFeatures:SetSize(160,25)
	showFeatures:SetSelected( LocalPlayer():GetInfoNum("npc_mover_gun_show_objects") == 1 )
	showFeatures.DoClick = function ()
		LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. LocalPlayer():EntIndex())
		LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_TOGGLE_VIEW_LINKACHORS)
		showFeatures:SetSelected( LocalPlayer():GetInfoNum("npc_mover_gun_show_objects") == 0 )
	end
	//showFeatures:SetValue( LocalPlayer():GetInfoNum("npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_TOGGLE_VIEW_LINKACHORS) )
	yPos = yPos + THIN_PAD + showFeatures:GetTall()
	
	local showWeapon = vgui.Create( "DButton", sectionPanel )
	showWeapon:SetPos(PADDING , yPos )
	showWeapon:SetText( "Toggle tool gun display" )
	showWeapon:SetSelected( LocalPlayer():GetInfoNum("npc_mover_gun_show_weapon") == 1 )	
	showWeapon:SetSize(160,25)
	showWeapon.DoClick = function ()
		showWeapon:SetSelected( LocalPlayer():GetInfoNum("npc_mover_gun_show_weapon") == 0 )	
		LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. LocalPlayer():EntIndex())
		LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_TOGGLE_VIEW_GUN)
	end
	yPos = yPos + THIN_PAD + showWeapon:GetTall()
	
	local chaseCamera = vgui.Create( "DButton", sectionPanel )
	chaseCamera:SetPos(PADDING , yPos )
	chaseCamera:SetText( "Toggle player chase camera" )
	chaseCamera:SetSelected( LocalPlayer():GetInfoNum("npc_mover_gun_third_person") == 1 )	
	chaseCamera:SetSize(160,25)
	chaseCamera.DoClick = function ()
		chaseCamera:SetSelected( LocalPlayer():GetInfoNum("npc_mover_gun_third_person") == 0 )	
		LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. LocalPlayer():EntIndex())
		LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_TOGGLE_VIEW_PERSP)
	end	
	yPos = yPos + PADDING + chaseCamera:GetTall()
	
	local user_instance = CreateLabel("User instance:", GREY, PADDING,yPos,sectionPanel)
	yPos = yPos + THIN_PAD + user_instance:GetTall()
	
	local dropWeaponsButton = vgui.Create( "DButton" , sectionPanel) 
	dropWeaponsButton:SetText( "Dispose inventory" )
	dropWeaponsButton:SetPos( PADDING, yPos) 
	dropWeaponsButton:SetSize( 160, 25 ) 
	dropWeaponsButton.DoClick = function ()
		LocalPlayer():ConCommand( "npc_mover_gun_entitIDfromclient " .. LocalPlayer():EntIndex())
		LocalPlayer():ConCommand( "npc_mover_gun_functIDfromclient " .. CatmullRomCams.SToolMethods.ActorMover.FUNCTION_ID_STRIP_WEAPONS)
	end					 
	yPos = yPos + (THIN_PAD * 2) + dropWeaponsButton:GetTall()

	local warning = CreateLabel("Warning:", Color(255,32,32,255), PADDING,yPos,sectionPanel)
	yPos = yPos + THIN_PAD + warning:GetTall()

	local warning_message = CreateLabel(
	"Disposing inventory will cause you to loose the all weapons\n" ..
	"as well as the current tool! You can restore the current tool\n" ..
	"at any time by pressing the \'R\' button", Color(128,128,128,255), PADDING,yPos,sectionPanel)
	yPos = yPos + THIN_PAD + warning_message:GetTall()


	
end

local function ShowProperties(userMessage)

	local callingVector = userMessage:ReadVector()
	local screenPos = callingVector:ToScreen()
	local secPanel = nil

	local propertiesDialog = vgui.Create( "DFrame" ) 
	propertiesDialog:SetPos( screenPos.x - 200,screenPos.y + 30) 
	propertiesDialog:SetSize( PADDING + CAT_WIDTH + PADDING + SEC_WIDTH + PADDING, HEIGHT ) 
	propertiesDialog:SetTitle( "Properties")
	propertiesDialog:SetVisible( true ) 
	propertiesDialog:SetDraggable( false )
	propertiesDialog:ShowCloseButton( true )
	propertiesDialog:MakePopup() 
    
	ANCHOR_Y = ANCHOR_Y + 20
	
	local catList = vgui.Create( "DPanelList", propertiesDialog ) 
	catList:SetPos( PADDING,TITLE_BAR_HEIGHT + PADDING ) 
	catList:SetSize( CAT_WIDTH, HEIGHT - (PADDING * 2) - TITLE_BAR_HEIGHT)
	catList:SetSpacing(1)
	catList:SetPadding(1)
	catList:EnableHorizontal(false)
	catList:EnableVerticalScrollbar(true)		 

	local rebuildSectionPanel = function()
		
		if secPanel then
			secPanel:SetVisible(false)
		end

		secPanel = vgui.Create( "DPanel", propertiesDialog) 
		secPanel:SetPos( PADDING + CAT_WIDTH + PADDING,TITLE_BAR_HEIGHT + PADDING ) 
		secPanel:SetSize( SEC_WIDTH, HEIGHT - (PADDING * 2) - TITLE_BAR_HEIGHT )
		secPanel.Paint = function()
			surface.SetDrawColor(50,50,50,255)
			surface.DrawRect(0,0,secPanel:GetWide(),secPanel:GetTall())
		end
	end

	local modeFunc = function () 
		rebuildSectionPanel()
		secPanel:Clear()
		lastPropertiesSection = 1
		DisplayModeSection(secPanel)
	end

	local utilityFunc = function () 
		rebuildSectionPanel()
		secPanel:Clear()
		lastPropertiesSection = 2
		DisplayUtilitySection(secPanel)
	end

	local closeFunc = function ()
		propertiesDialog:SetVisible(false)
	end

	catList:AddItem(CreateLabel("Main",GREY, PADDING, ANCHOR_Y, catList))
	catList:AddItem(CreateButton("Director Mode", modeFunc, CAT_WIDTH,15, false, catList))
	catList:AddItem(CreateButton("Utilities", utilityFunc, CAT_WIDTH,15, false, catList))
	catList:AddItem(CreateButton("Close", closeFunc, CAT_WIDTH,15, false, catList))

	//Show last playback section
	if lastPropertiesSection == 1 then
		modeFunc()
	elseif lastPropertiesSection == 2 then
		utilityFunc()
	end
end

usermessage.Hook( "ShowProperties", ShowProperties)